package org.koyi.fragments;

import java.util.ArrayList;
import java.util.List;

import org.koyi.birrapp.R;
import org.koyi.entities.Bares;
import org.koyi.entities.BaresCervezasTapasVasos;
import org.koyi.singleton.BaresSingleton;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class BuscarFragment extends Fragment {
	
	private Spinner spinner = null;
	
	private EditText nombreBar = null;
	private EditText cerveza = null;
	private EditText precioMax = null;
	private Button buscar = null;
	private static List<Bares> listaBares = null;
	private List<Bares> listaBaresResultado = null;
	private BuscarListFragment buscarListFragment = null;
	
	private String paramBar = null;
	private String paramCerveza = null;
	private String paramVaso = null;
	private Double paramPrecioMax = null;
	
	private boolean borrar = true;
	
	private View rootView = null;
	
	@Override
    public void onActivityCreated(Bundle savedInstanceState) 
	{
        super.onActivityCreated(savedInstanceState);
        
        getActivity().setTitle("Buscador Locales");
        
        listaBares = new ArrayList<Bares>();
        listaBaresResultado = new ArrayList<Bares>();
        listaBares = BaresSingleton.getListaBares();
        
        buscar = (Button) getActivity().findViewById(R.id.buttonBuscar);
        nombreBar = (EditText) getActivity().findViewById(R.id.paramBar);
        cerveza = (EditText) getActivity().findViewById(R.id.paramCerveza);
        precioMax = (EditText) getActivity().findViewById(R.id.paramPrecioMax);
        spinner = (Spinner) getActivity().findViewById(R.id.paramVaso);
        
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
        	        														 R.array.valores_spinner_vasos,
        	        														 R.layout.spinner_style);
        spinner.setAdapter(adapter);
        
        buscar.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {				
				consulta();
				
				InputMethodManager inputManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
				inputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(),
                        						     InputMethodManager.HIDE_NOT_ALWAYS);
			}
		});
        
        FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
		buscarListFragment = buscarListFragment.initialize(listaBaresResultado);
		fragmentTransaction.replace(R.id.buscarListFragment, buscarListFragment);
		fragmentTransaction.commit();
    }
	
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        
        if(rootView == null)
		{
			rootView = inflater.inflate(R.layout.buscar_fragment, container, false);
		}
		else
		{
			((ViewGroup)rootView.getParent()).removeAllViews();
		}
        
        return rootView;
    }
	
	private void consulta()
	{
		listaBaresResultado.clear();
		listaBaresResultado.addAll(listaBares);
		
		paramBar = nombreBar.getText().toString();
		paramCerveza = cerveza.getText().toString();
		paramVaso = spinner.getSelectedItem().toString();
		
		if(precioMax.getText().toString().isEmpty())
		{
			paramPrecioMax = Double.valueOf("0");
		}
		else
		{
			paramPrecioMax = Double.valueOf(precioMax.getText().toString());
		}
		
		for(Bares bar : listaBares)
		{	
			
			borrar = true;
			
			if(paramBar != null && !paramBar.isEmpty())
			{
				if(!bar.getNombre().equalsIgnoreCase(paramBar))
				{
					listaBaresResultado.remove(bar);
				}
			}
			
			if(listaBaresResultado.contains(bar))
			{
				if(paramCerveza != null && !paramCerveza.isEmpty())
				{
					for(BaresCervezasTapasVasos bares : bar.getListaBirras())
					{
						if(bares.getNombreCerveza().equalsIgnoreCase(paramCerveza)){
			
							borrar = false;
						}
					}
				}
				
				if(borrar && paramCerveza != null && !paramCerveza.isEmpty())
				{
					listaBaresResultado.remove(bar);
				}
			}
			
			if(listaBaresResultado.contains(bar))
			{
				borrar = true;
				
				if(paramVaso != null && !paramVaso.isEmpty())
				{
					for(BaresCervezasTapasVasos bares : bar.getListaBirras())
					{
						if(bares.getNombreVaso().equalsIgnoreCase(paramVaso)){
			
							borrar = false;
						}
					}
				}
				
				if(borrar && !paramVaso.equalsIgnoreCase("Seleccionar..."))
				{
					listaBaresResultado.remove(bar);
					borrar = true;
				}
			}
			
			if(listaBaresResultado.contains(bar))
			{
				borrar = true;
				
				if(paramPrecioMax.compareTo(Double.valueOf("0")) > 0)
				{
					for(BaresCervezasTapasVasos bares : bar.getListaBirras())
					{	
						if(bares.getPrecio().compareTo(paramPrecioMax) <= 0){
			
							borrar = false;
						}
					}
				}
				
				if(borrar && paramPrecioMax.compareTo(Double.valueOf("0")) > 0)
				{
					listaBaresResultado.remove(bar);
					borrar = true;
				}
			}
		}
		
		if(listaBaresResultado != null)
        {
			FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
			buscarListFragment = BuscarListFragment.initialize(listaBaresResultado);
			fragmentTransaction.replace(R.id.buscarListFragment, buscarListFragment);
			fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
			fragmentTransaction.addToBackStack(null);
			fragmentTransaction.commit(); 
        }
	}
}
